package com.hzw.saas.common.storage.enums;

import java.util.Objects;

import cn.hutool.core.lang.Assert;

/**
 * 资源存储模式
 *
 * @author zzl
 * @since 07/03/2021
 */
public enum StorageModeEnum {

    SAVE_IN_LOCAL(1, "仅本地存储"),

    SAVE_IN_CLOUD(2, "仅云端存储"),

    SAVE_IN_BOTH(3, "双端存储"),

    AVAILABLE_IN_LOCAL(10, "使本地存在"),

    AVAILABLE_IN_CLOUD(20, "使云端存在"),

    CLEAR_FILE(-1, "清理所有存储"),

    CLEAR_ALL(-2, "清理所有存储与数据库记录")
    ;
    private final Integer code;
    private final String text;

    StorageModeEnum(Integer code, String text) {
        this.code = code;
        this.text = text;
    }

    public Integer code() {
        return code;
    }

    public String text() {
        return text;
    }

    public static boolean isValid(Integer code) {
        StorageModeEnum instance = instance(code);
        return Objects.nonNull(instance);
    }

    public static void assertValid(Integer code) {
        StorageModeEnum instance = instance(code);
        Assert.notNull(instance, "存储模式异常");
    }

    public static StorageModeEnum instanceValid(Integer code) {
        StorageModeEnum instance = instance(code);
        return Objects.isNull(instance) ? StorageModeEnum.SAVE_IN_CLOUD : instance;
    }

    public static StorageModeEnum instance(Integer code) {
        for (StorageModeEnum value : StorageModeEnum.values()) {
            if (Objects.equals(value.code(), code))
                return value;
        }
        return null;
    }

    public static StorageModeEnum instance(Integer code, StorageModeEnum defaultValue) {
        StorageModeEnum instance = instance(code);
        return Objects.isNull(instance) ? defaultValue : instance;
    }


}
